package com.laikan.reader.apirequester;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.laikan.library.httpvolley.engine.HttpApiRequestEngine;
import com.laikan.library.httpvolley.engine.Requester;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.httpvolley.helper.VolleyErrorHandler;
import com.laikan.library.httpvolley.response.HttpResponse;
import com.laikan.library.managers.ToastMgr;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.comment.APIProtocol;
import com.laikan.reader.comment.Constants;
import com.laikan.reader.core.LaikanApplication;
import com.laikan.reader.core.bean.BaseDataBean;
import com.laikan.reader.core.bean.BatchChapterInfo;
import com.laikan.reader.core.events.LaiKanEvent;
import com.laikan.reader.core.utils.LoadingDialog;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by anrun on 9/29/16.
 */
public class DesChapterBuyRequester {

    public static void getChapterCountData(String startid, String count, String bookid, final LoadingDialog mloadingDialog) {

        Map<String, String> map = new HashMap<>();
        map.put("sd", SPUtil.getString("token", ""));
        map.put("bookId", bookid);
        map.put("startChapeterID", startid);
        map.put("count", count);
        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() {
                mloadingDialog.show();
            }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                BaseDataBean<BatchChapterInfo> batchChapterInfo;
                Gson gson = new Gson();
                Type type = new TypeToken<BaseDataBean<BatchChapterInfo>>() {}.getType();
                try {
                    //System.out.println(httpResponse.responseContent);
                    //System.out: {"data":{"endChapter":"第38章  天火","beginChapter":"第19章  金针刺穴","userTicket":0,"chapterCount":20,"discountMoney":274,"realMoney":315,"userMoney":0.0},"result":0,"error_no":602,"error_msg":"余额不足请充值"}
                    //System.out: {"data":{"endChapter":"第118章  孤枕难眠","beginChapter":"第19章  金针刺穴","userTicket":0,"chapterCount":100,"discountMoney":1011,"realMoney":1515,"userMoney":52.19},"result":1,"error_no":0,"error_msg":""}
                    batchChapterInfo = gson.fromJson(httpResponse.responseContent, type);
                    if (batchChapterInfo != null) {

                        if ("602".equals(batchChapterInfo.getError_no()) || Constants.RESULT_OK.equals(batchChapterInfo.getResult())) {

                            LaiKanEvent event = LaiKanEvent.getInstance();
                            event.setEventID(LaiKanEvent.EVENT_ID_GET_PREPAY_INFO);
                            event.setElves(batchChapterInfo);
                            LaikanApplication.postEvent(event);
                        }else {
                            ToastMgr.showShortToast(batchChapterInfo.getError_msg());
                        }
                    }else {
                        ToastMgr.showShortToast(R.string.laikan_data_access_failed);
                    }
                }catch (Exception e) {
                    ToastMgr.showShortToast(R.string.laikan_failed_to_parse);
                    return;
                }
            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {

            }

            @Override
            public void onFinish() {
                mloadingDialog.dismiss();
            }
        };

        Requester requester = new Requester();
        requester.setUrl(APIProtocol.laikan_deschapterbuy_pilianginfo);
        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpPostRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpPostRequest(APIProtocol.laikan_deschapterbuy_pilianginfo, map, null, null, requestListener);
    }

    /**
     * 从网络获取数据
     */
//    private void getDate(String bookid, String startChapeterID, final LoadingDialog mloadingDialog) {
//        mloadingDialog.show();
//        Map<String, String> map = new HashMap<>();
//        map.put("sd", SPUtil.getString("token", ""));
//        map.put("bookId", bookid);
//        map.put("startChapeterID", startChapeterID);
//        RequestListener requestListener = new RequestListener() {
//            @Override
//            public void onPreExecute() { KLog.e("---------volley onPreExecute!!!"); }
//
//            @Override
//            public void onSuccess(HttpResponse httpResponse) {
//                mloadingDialog.dismiss();
//                Gson gson = new Gson();
//                Type type = new TypeToken<BaseDataBean<Des_Chapter_Bean>>() {}.getType();
//                try {
//                    BaseDataBean<Des_Chapter_Bean> result = gson.fromJson(httpResponse.responseContent, type);
//                    String error_no = result.getError_no();
//                    if (result.getResult().equals("1")) {
//                        SPUtil.putBoolean("login_laikan_setting", true);
//                        if (Integer.valueOf(result.getData().getChapterCount()) == 0) {
//                            ToastMgr.showShortToast(R.string.deschapterbuy_none_chapter_can_buy);
//                            finish();
//                        }else {
//                            if (notiPage) {
//                                getData11();
//                            }
//                            setData(result);
//                        }
//
//                    }else if (result.getResult().equals(Constants.RESULT_NEED_LOGIN)) {
//                        ToastMgr.showShortToast(result.getError_msg());
//                        Intent intent = new Intent(mContext, LoginActivity.class);
//                        startActivity(intent);
//                    }
//                    if (Constants.RESULT_TICKET_NOT_ENOUGH.equals(error_no) || Constants.RESULT_BALANCE_NOT_ENOUGH.equals(error_no)) {
//                        ToastMgr.showShortToast(result.getError_msg());
//                    }
//                    if ("102".equals(error_no)) {
//                        ToastMgr.showShortToast(result.getError_msg());
//                        finish();
//                    }
//                }catch (Exception e) {
//                    ToastMgr.showShortToast(R.string.laikan_failed_to_parse);
//                    return;
//                }
//
//            }
//
//            @Override
//            public void onError(VolleyErrorHandler errorHandler) {
//                mloadingDialog.dismiss();
//                ToastMgr.showShortToast(R.string.laikan_network_not_to_force);
//                finish();
//                KLog.e("网络不给力，请重试。13");
//            }
//
//            @Override
//            public void onFinish() {
//
//            }
//        };
//        HttpApiRequestEngine.getInstance().httpPostRequest(APIProtocol.laikan_deschapterbuy, map, null, null, requestListener);
//    }
}
